package com.example.tests;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.example.obj.GroupObject;
import com.example.obj.GroupSet;

public class Test_05_GroupModification extends TestBase{
	
	Random rnd = new Random();
	
	@BeforeMethod
	public void init(){
		if (manager.getGroupHelper().getGroupsCouche().size() == 0) { 
			log.info("Before: Start group modification");
			log.info("There are no groups for modification. Try to create one");			
			manager.getGroupHelper().createGroup(new GroupObject().setName("name"));
		}
	}
	
	@Test(priority = 3)
	public void testGroupCanBeModified() throws Exception {		
		log.info("Start testGroupCanBeModified");
		GroupObject modifiedGroup = new GroupObject()
			.setName("testgrEDITname")
			.setHeader("testgrEDITheader")
			.setFooter("testgrEDITfooter");					
		GroupSet oldGroupSet = manager.getGroupHelper().getGroupsCouche();
		assertThat(oldGroupSet.size() > 0, is(true));
		log.info("Old groups list: " + oldGroupSet);
		GroupObject oldGroup = oldGroupSet.getElement(rnd.nextInt(oldGroupSet.size()));	
		manager.getGroupHelper().modifyGroup(oldGroup.id, modifiedGroup);
		GroupSet newGroupSet = manager.getGroupHelper().getGroupsCouche();
		log.info("New groups list: " + newGroupSet);
		log.info("Start assertions: size, lists' elements");		
		assertThat(newGroupSet.size(), equalTo(oldGroupSet.size()));
		assertThat(newGroupSet, equalTo(oldGroupSet.without(oldGroup).with(modifiedGroup)));
		log.info("Stop assertions");
	}
	
	@AfterMethod
	public void testUIConfirmWithDB(){
		log.info("After testGroupCanBeModified");
		GroupSet groupsDB = manager.getGroupHelper().getGroupsCouche();
		GroupObject groupDB = groupsDB.getElement(rnd.nextInt(groupsDB.size()));		
		log.info("Group from DB: " + groupDB);
		GroupObject groupUI = manager.getGroupHelper().getGroupUI(groupDB.id);
		log.info("Group from UI: " + groupUI);
		assertThat(groupUI, equalTo(groupDB));
	}
}
